package com.eyas.business.service.sellinglessons;

import com.eyas.business.config.webmvc.exception.custom.uncheck.EyasBusinessException;
import com.eyas.business.config.webmvc.interceptor.LoginInterceptor;
import com.eyas.business.model.jdbcEntity.table.RecordInfo;
import com.eyas.business.model.jdbcEntity.table.UserInfo;
import com.eyas.business.model.jpa.AdminInfo;
import com.eyas.business.model.pojo.sellinglessons.ParamDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static com.eyas.core.jdbc.utils.CommonJdbcUtils.commonJdbcUtils;

/**
 * @Auther: 王龙龙
 * @Date: 2019/3/30 11:29
 * @Description:
 */
@Service
@Transactional
public class SellingLessonsServiceImpl implements SellingLessonsService {
    @Override
    public void updateAppointments(List<ParamDTO> list, String status, HttpSession session) {
        List<Object[]> args = new ArrayList<>();
        if(list==null||list.size()==0)
            throw new EyasBusinessException("请选择会员");
        if(status.equals("2")) {//签到
            for(ParamDTO dto:list) {
                Integer[] integer = new Integer[1];
                integer[0] = dto.getAppointmentid();
                args.add(integer);
            }
            commonJdbcUtils.batchUpdate("update appointmentinfo set status='2' where appointmentid=?",args);
        }else if(status.equals("4")) {//销课
            RecordInfo recordInfo = commonJdbcUtils.queryObject("select * from recordinfo where recordid=?",RecordInfo.class,list.get(0).getRecordid());
            if(recordInfo==null||recordInfo.getRecordhour()==null||recordInfo.getRecordhour()==0)
                throw new EyasBusinessException("这次的开课没有维护课时消耗数量，请先维护");
            int recordhour = recordInfo.getRecordhour();
            AdminInfo adminInfo = (AdminInfo) session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME);

            //校验选中的会员是否有足够的课时
            List<Object[]> user_list = new ArrayList<>();
            for(ParamDTO dto:list) {
                UserInfo userInfo = commonJdbcUtils.queryObject("select * from userinfo where userid=?",UserInfo.class,dto.getUserid());
                int user_rest_class = userInfo.getRestclass()==null?0:userInfo.getRestclass();
                int user_tiyan_class = userInfo.getTiyannums()==null?0:userInfo.getTiyannums();
                if(user_rest_class+user_tiyan_class<=0||user_rest_class+user_tiyan_class-recordhour<0)
                    throw new EyasBusinessException("会员["+userInfo.getUsername()+"]剩余课时不足，请先去充值");

                Object[] objs = new Object[5];
                Object[] objs2 = new Object[3];
                //设置课时削减数量
                if(user_rest_class>=recordhour) {
                    objs2[0] = recordhour;
                    objs2[1] = 0;
                }else {
                    objs2[0] = user_rest_class;
                    objs2[1] = user_rest_class-recordhour+user_tiyan_class;
                }

                objs2[2] = userInfo.getUserid();
                user_list.add(objs2);
                objs[0] = recordhour;
                objs[1] = user_rest_class-recordhour>=0?0:(user_rest_class-recordhour)*-1;
                objs[2] = adminInfo.getAdminnid();
                objs[3] = new Date();
                objs[4] = dto.getAppointmentid();
                args.add(objs);
            }
            //更新约课情况
            commonJdbcUtils.batchUpdate("update appointmentinfo set status='2',consumes=?,consumest=?,operationid=?,operationtime=? where appointmentid=?",args);
            //更新会员剩余课时
            commonJdbcUtils.batchUpdate("update userinfo set restclass=restclass-?,tiyannums=tiyannums-? where userid=?",user_list);

        }else if(status.equals("1")) {
            for(ParamDTO dto:list) {
                Integer[] integer = new Integer[1];
                integer[0] = dto.getAppointmentid();
                args.add(integer);
            }
            commonJdbcUtils.batchUpdate("update appointmentinfo set status='1' where appointmentid=?",args);
        }
    }
}
